package com.shengyuan.manage_os.json;

import android.content.Context;

import com.shengyuan.manage_os.sqlite.SQLiteTools;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import oa.database.bean.Employee;

/**
 * Created by Administrator on 2017/1/5.
 */

public class JsonUtils {

    public static void compleJson(Context context,int resource){
        String ddd = readLocalJson(context,resource);

        try {
            JSONObject json = new JSONObject(ddd);
            JSONArray bumen = json.optJSONArray("部门");
            for(int i = 0; i < bumen.length();i++) {
                JSONObject aaa = bumen.optJSONObject(i);
                JSONArray yuangong = aaa.optJSONArray("员工");
                for (int j = 0; j < yuangong.length(); j++) {
                    JSONObject role = yuangong.optJSONObject(j);
                    String s = role.optString("UserName");
                    System.out.println("**************" + s);
                }
            }
        }catch (Exception e){
            System.out.println("********========******"+e);
        }
    }
    public static void xunhuan(JSONArray array,long pid){
        List<Employee> list = new ArrayList<>();
        for(int i = 0; i < array.length();i++) {
            JSONObject aaa = array.optJSONObject(i);
            Employee employee = new Employee();
            employee.setUserName(aaa.optString("name"));
            employee.setUid(Long.parseLong(aaa.optString("id")));
            employee.setPid(pid);
            list.add(employee);
        }
        SQLiteTools.addEmployeeS(list);
    }
    public static void compleJson2(Context context,int resource){
        String ddd = readLocalJson(context,resource);

        try {
            JSONObject json = new JSONObject(ddd);
            JSONArray bumen = json.optJSONArray("行政人事部");
            JSONArray bumen2 = json.optJSONArray("网络工程部");
            JSONArray bumen3 = json.optJSONArray("财务审计部");
            JSONArray bumen4 = json.optJSONArray("机械运输部");
            JSONArray bumen5 = json.optJSONArray("市场发展部");
            JSONArray bumen6 = json.optJSONArray("工程技术部");
            JSONArray bumen7 = json.optJSONArray("经营计划部");
            JSONArray bumen8 = json.optJSONArray("副总经理室");
            JSONArray bumen9 = json.optJSONArray("总经理办公室");
            JSONArray bumen0 = json.optJSONArray("综合值班室");
            SQLiteTools.addDepartment(1001,"行政人事部",165437327374L,"剋加第");
            SQLiteTools.addDepartment(1002,"网络工程部",165437327374L,"剋加第");
            SQLiteTools.addDepartment(1003,"财务审计部",165437327374L,"剋加第");
            SQLiteTools.addDepartment(1004,"机械运输部",165437327374L,"剋加第");
            SQLiteTools.addDepartment(1005,"市场发展部",165437327374L,"剋加第");
            SQLiteTools.addDepartment(1006,"工程技术部",165437327374L,"剋加第");
            SQLiteTools.addDepartment(1007,"经营计划部",165437327374L,"剋加第");
            SQLiteTools.addDepartment(1008,"副总经理室",165437327374L,"剋加第");
            SQLiteTools.addDepartment(1009,"总经理办公室",165437327374L,"剋加第");
            SQLiteTools.addDepartment(1010,"综合值班室",165437327374L,"剋加第");
           xunhuan(bumen,1001);
            xunhuan(bumen2,1002);
            xunhuan(bumen3,1003);
            xunhuan(bumen4,1004);
            xunhuan(bumen5,1005);
            xunhuan(bumen6,1006);
            xunhuan(bumen7,1007);
            xunhuan(bumen8,1008);
            xunhuan(bumen9,1009);
            xunhuan(bumen0,1010);
        }catch (Exception e){
            System.out.println("********========******"+e);
        }
    }

    public static String readLocalJson(Context context,int resource){

        String result = "";
        try {
            InputStreamReader isr = new InputStreamReader(context.getResources().openRawResource(resource), "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
            br.close();
            isr.close();
            result = builder.toString();
        }catch (Exception e){
            System.out.println("*********#################*****"+e);
        }
        return result;
    }
}
